package com.network.service;

import com.network.common.JdbcUtil;
import com.network.dao.EquipmentDao;
import com.network.dao.FileSaveDao;
import com.network.entity.Dept;
import com.network.entity.Equipment;
import com.network.entity.FileSave;
import com.network.entity.User;
import com.network.qo.FileQo;
import com.network.qo.PageQo;
import com.network.util.CommonUtils;
import com.network.vo.EquipmentVo;
import com.network.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

@Service
public class EquipmentService
{

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    JdbcUtil jdbcUtil;

    @Autowired
    EquipmentDao equipmentDao;

    @Autowired
    FileSaveDao fileSaveDao;

    public List<EquipmentVo> getEquipByJob(Long id)
    {
        //根据任务ID  获取 设备采集内容  每种文件类型采集的方式不一样 所以连接了 文件类型表
        String sql="select A.id,A.`name`,A.ip,A.`port`,A.username,A.brand,A.`password`,\n" +
                "(SELECT typename from file_type_t where id=C.file_type_id) as typename,\n" +
                "C.get_method as method,C.file_name as fileName,C.file_path as filePath from equipment_t as A \n" +
                "LEFT JOIN job_equipment_t as B ON B.equip_id=A.id \n" +
                "LEFT JOIN file_type_equipment_t as C ON C.equipment_id=A.id\n" +
                "WHERE A.state=0 and  B.job_id=?";



        List<EquipmentVo> list= jdbcTemplate.query(sql, new Object[]{id}, new int[]{Types.BIGINT}, new RowMapper<EquipmentVo>() {
            @Override
            public EquipmentVo mapRow(ResultSet rs, int rowNum) throws SQLException {

                EquipmentVo equipmentVo =null;
                equipmentVo=new EquipmentVo();
                equipmentVo.setId(rs.getLong("id"));
                equipmentVo.setName(rs.getString("name"));
                equipmentVo.setIp(rs.getString("ip"));
                equipmentVo.setPort(rs.getString("port"));
                equipmentVo.setUsername(rs.getString("username"));
                equipmentVo.setBrand(rs.getString("brand"));
                equipmentVo.setPassword(rs.getString("password"));
                equipmentVo.setTypename(rs.getString("typename"));
                equipmentVo.setMethod(rs.getString("method"));
                equipmentVo.setFileName(rs.getString("fileName"));
                equipmentVo.setFilePath(rs.getString("filePath"));

                return equipmentVo;
            }
        });

        return list;
    }

    public Page<Equipment> getEquipByPage(PageQo pageQo)
    {

        //根据创建时间排序
        Sort sort=new Sort(Sort.Direction.DESC,"createTime");
        Pageable pageable=new PageRequest(pageQo.getPage(),pageQo.getLimit(),sort);
        return equipmentDao.findAll((root,query,criteriaBuilder) ->{
            List<Predicate> predicateList=new ArrayList<Predicate>();
            query.where(predicateList.toArray(new Predicate[predicateList.size()]));
            return query.getRestriction();
        },pageable);

    }

    public PageResult getFileByEquipIp(FileQo fileQo)
    {
        String select="select A.id,A.equipment_name as equipmentName,A.ip," +
                "B.brand,A.file_name as fileName,A.file_path as filePath,A.file_size as fileSize," +
                "A.file_suffix as fileSuffix,A.file_type_name as fileTypeName,date_format(A.create_time, '%Y-%m-%d %H:%i:%S') as createTime";
        StringBuffer from=new StringBuffer("from file_save_t as A LEFT JOIN equipment_t as B ON A.ip=B.ip");

        from.append(" where 1=1 ");
        if (CommonUtils.isNotNull(fileQo.getIp()))
        {
            from.append(" and  A.ip='"+fileQo.getIp()+"'");
        }
        if (CommonUtils.isNotNull(fileQo.getFileType()))
        {
            from.append(" and A.file_type_name='"+fileQo.getFileType()+"'");
        }
        if (CommonUtils.isNotNull(fileQo.getStartTime()))
        {
            from.append(" and A.create_time>='"+fileQo.getStartTime()+"'");
        }
        if (CommonUtils.isNotNull(fileQo.getEndTime()))
        {
            from.append(" where A.create_time<='"+fileQo.getEndTime()+"'");
        }
        from.append(" order by  A.create_time desc");
        PageResult pageResult= jdbcUtil.pageSql(fileQo.getPage(),fileQo.getLimit(),select,from.toString());

        return pageResult;

    }
}
